package com.theaf.basemodule.service.impl;

import com.theaf.basemodule.common.api.PositionService;
import com.theaf.basemodule.common.condition.PosCondition;
import com.theaf.basemodule.common.dto.PositionDTO;
import com.theaf.basemodule.common.model.SysPosition;
import com.theaf.basemodule.common.model.SysResource;
import com.theaf.basemodule.common.vo.PositionVO;
import com.theaf.basemodule.dao.OrganDAO;
import com.theaf.basemodule.dao.PositionDAO;
import com.theaf.basemodule.dao.UserDAO;
import com.theaf.basemodule.service.constants.StateConstant;
import com.theaf.core.cache.RedisHelper;
import com.theaf.core.repo.specification.SqlBuilder;
import com.theaf.core.repo.specification.SqlOperator;
import com.theaf.core.repo.specification.SqlValue;
import com.theaf.core.utils.BeanMapper;
import com.theaf.core.utils.SmExceptionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:songwenke
 * @Description:岗位服务
 * @Date:create in 17:04 2018/3/7
 * @Modified By:
 **/
@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    private PositionDAO positionDAO;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private OrganDAO organDAO;
    @Override
    public Page<PositionDTO> findCustom(PosCondition posCondition) {
        return positionDAO.findCustom(posCondition).map(n-> BeanMapper.map(n,PositionDTO.class));
    }

    @Override
    public PositionDTO findById(Long id) {
        SysPosition sysPosition = positionDAO.findOne(id);
        PositionDTO positionDTO = BeanMapper.map(sysPosition,PositionDTO.class);
        positionDTO.setPosIds(sysPosition.getResources().parallelStream()
                .map(n->n.getId()).collect(Collectors.toList()));
        positionDTO.setPosNames(sysPosition.getResources()
                .parallelStream().map(n->n.getName()).reduce((a,b)->a+","+b).get());
        if(sysPosition.getOrgRange()!=null) {
            positionDTO.setOrgRangeName(organDAO.findOne(sysPosition.getOrgRange()).getFullName());
        }
        return positionDTO;
    }

    @Override
    public List<PositionDTO> findByOrganType(String organType) {
        List<SysPosition> sysPositions = positionDAO.findAll(
                SqlBuilder.start("orgType",SqlValue.parse(organType)).gen());
        return sysPositions.parallelStream().map(
                n->BeanMapper.map(n,PositionDTO.class)).collect(Collectors.toList());
    }


    @Override
    public void delete(Long id) {
        if(userDAO.count(SqlBuilder.start("posId",id)
                .and("state", SqlOperator.OPER.NE, StateConstant.DELETE)
                .gen())>0){
            throw SmExceptionHelper.sysCheckException(id+"此岗位已经被使用");
        }
        positionDAO.delete(id);
    }

    @Override
    public void save(PositionVO positionVO) {
        SysPosition sysPosition = positionDAO.findByIdNewIfisNull(positionVO.getId());
        BeanMapper.copy(positionVO,sysPosition);
        sysPosition.setResources(positionVO.getAuths().parallelStream()
                .map(n->new SysResource(n)).collect(Collectors.toList()));
        //清除缓存
        RedisHelper.clearByPrefix(UserServiceImpl.REDIS_DATA_RANGE);
        positionDAO.save(sysPosition);
    }
}
